<?php
/**
 * Created by PhpStorm.
 * User: front
 * Date: 2018/8/3
 * Time: 16:39
 */

namespace App\Services;

use App\Exceptions\ApiException;
use App\Models\MediaLikeStatic;
use App\Models\User;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class ActivityService
{

    /*
     * 获取排行榜
     * */
    public function get_activity_ranking($request, $activity_id)
    {
        $arr = [];
        $flag = false;
        $ranking_number = 0; // 名次

        $type = IT('type', 1);
        $date_format = IT('date_format');
        $ticket = IT('ticket');

        // 设置周期默认值
        if(empty($date_format)){
            $date_format = date("W", time());
            if($type == 2)
                $date_format = date("Ym", time());
        }

        // 判断用户是否登录
        $users_info = checkTicket($ticket);

        $where[] = ['type', '=', $type];
        $where[] = ['activity_id', '=', $activity_id];
        $where[] = ['date_format', '=', $date_format];
        $media_like_ranking = MediaLikeStatic::where($where)->orderBy('like_num', 'desc')->orderBy('update_time', 'asc')->orderBy('id', 'asc')->limit(5)->with('user')->get();

        // 校验当前用户是否在当前排名
        foreach ($media_like_ranking as $key=>$value){
            $temp_arr = [];
            $ranking_number++;
            $temp_arr['one_self'] = 0;
            if(!empty($users_info) &&  $value->uid == $users_info['uid']){
                $temp_arr['one_self'] = 1;
                $flag = true;
            }
            $temp_arr['ranking'] = $ranking_number;
            $temp_arr['avatar'] = empty($value->user->avatar)  ? '' : getSignUrl($value->user->avatar) ;
            $temp_arr['like_num'] = $value->like_num;
            $temp_arr['uid'] = $value->uid;
            $temp_arr['name'] = $value->user->name;
            $temp_arr['update_date'] =date("m-d H:i:s", $value->update_time);

            $arr[] = $temp_arr;
        }

        // 获取个人当前排行
        if(!empty($users_info) && !$flag){
            $user_rows = User::select(['id', 'name','avatar'])->where([['id', '=', $users_info['uid']]])->first();
            $media_like_static_model = new MediaLikeStatic;
            $sql = "SELECT * FROM (SELECT a.*, (@rowno:=@rowno+1) as rowno  FROM ".$media_like_static_model->getTable().
            " a,(select (@rowno:=0)) b WHERE a.activity_id={$activity_id} and a.type={$type} and a.date_format={$date_format} ORDER BY a.like_num DESC,a.update_time ASC,a.id ASC) c WHERE c.uid={$users_info['uid']}";
            $user_ranking_rows = DB::select($sql);
            if(!empty($user_ranking_rows)){
                $arr[] = [
                    'one_self'=>1,
                    'avatar'=>empty($user_rows->avatar)  ? '' : getSignUrl($user_rows->avatar),
                    'ranking'=>((int)$user_ranking_rows[0]->rowno),
                    'like_num'=>(int)$user_ranking_rows[0]->like_num,
                    'uid'=>(int)$user_ranking_rows[0]->uid,
                    'name'=>empty($user_rows)? '': $user_rows->name,
                    'update_date'=>date("m-d H:i:s", $user_ranking_rows[0]->update_time)];
            }
        }
        return $arr;
    }

}